package com.acme.domain.account;

import com.acme.exceptions.OverDraftLimitExceededException;

import java.io.Serializable;

public class CheckingAccount extends AbstractAccount implements Serializable {
    private double overdraft;

    public CheckingAccount(final double amount, final double overdraft) {
        super(amount);
        if (overdraft < 0) {
            throw new IllegalArgumentException("Balance cannot be less than 0.");
        }
        this.overdraft = overdraft;
    }

    @Override
        public void withdraw(double x) throws OverDraftLimitExceededException {
            if (x > maximumAmountToWithdraw()) {
                throw new OverDraftLimitExceededException(getBalance(), maximumAmountToWithdraw());
            }
            if (getBalance() >= x) {
                balance -= x;
            } else if (getBalance() + overdraft >= x) {
                x -= getBalance();
                balance = 0;
                overdraft -= x;
            }
        }

        public double maximumAmountToWithdraw() {
            return getBalance() + overdraft;
        }

    @Override
    public void deposit(final double amount) {
        if (amount > 0) {
            this.balance += amount;
        } else {
            throw new IllegalArgumentException("amount ca not be negative");
        }
    }

    public AccountType getAccountType() {
        return AccountType.CHECKING;
    }

    public double getOverdraft() {
        return overdraft;
    }

    @Override
    public String toString() {
        return "Checking account with " + overdraft + "$ overdraft. Amount: "
                + getBalance() + "$";

    }
}